const Comment = require("../db/model/comments")
const { SuccessModel, ErrorModel } = require("../utils/resModel")

class CommentCtl {
    /**
     * 获取评论列表
     * @param {*} ctx 
     */
    async find(ctx) {
        const { per_page = 10 } = ctx.query
        const perPage = Math.max(parseInt(per_page), 1)
        const page = Math.max(parseInt(ctx.query.page), 1) - 1
        const q = new RegExp(ctx.query.q)
        const { questionId, answerId } = ctx.params
        const { rootCommentId } = ctx.query
        const list = await Comment
            .find({ content: q, questionId, answerId, rootCommentId })
            .limit(perPage).skip(page * perPage)
            .populate("commentator replyTo")
        ctx.body = new SuccessModel(list)
    }

    /**
     * 根据id查询一个评论
     * @param {*} ctx 
     */
    async findById(ctx) {
        const { fields = "" } = ctx.query
        let selectFields = fields.split(";").filter(f => f).map(f => ' +' + f).join(" ")
        const comment = await Comment.findById(ctx.params.id).select(selectFields).populate("commentator")
        ctx.body = new SuccessModel(comment)
    }

    /**
     * 检查该评论是否属于该用户
     * @param {*} ctx 
     * @param {*} next 
     */
    async checkCommentator(ctx, next) {
        const { comment } = ctx.state
        if (comment.commentator.toString() !== ctx.state.user._id) {
            ctx.throw(403, "没有权限操作")
        }
        await next()
    }

    /**
     * 查询评论是否存在
     * @param {*} ctx 
     * @param {*} next 
     */
    async checkCommentExist(ctx, next) {
        const comment = await Comment.findById(ctx.params.id).select("+commentator")
        if (!comment) {
            ctx.throw(404, "评论不存在")
        }
        // 只有删改查答案的时候才检查此逻辑，赞和踩答案的时候不检查此逻辑
        if (ctx.params.questionId && comment.questionId !== ctx.params.questionId) {
            ctx.throw(404, "该问题下没有此评论")
        }
        if (ctx.params.answerId && comment.answerId !== ctx.params.answerId) {
            ctx.throw(404, "该回答下没有此评论")
        }
        ctx.state.comment = comment
        await next()
    }

    /**
     * 创建一个评论
     * @param {*} ctx 
     */
    async create(ctx) {
        ctx.verifyParams({
            content: { type: "string", required: true },
            rootCommentId: { type: "string", required: false },
            replyTo: { type: "string", required: false },
        })
        const commentator = ctx.state.user._id
        const { questionId, answerId } = ctx.params
        const comment = await new Comment({ ...ctx.request.body, commentator, questionId, answerId }).save()
        ctx.body = new SuccessModel(comment)
    }

    /**
     * 修改一个评论
     * @param {*} ctx 
     */
    async update(ctx) {
        ctx.verifyParams({
            content: { type: "string", required: false }
        })
        const { content } = ctx.request.body
        const comment = await ctx.state.comment.update({ content })
        ctx.body = new SuccessModel(comment)
    }

    /**
     * 删除一个评论
     * @param {*} ctx 
     */
    async deleteComment(ctx) {
        const comment = await Comment.findByIdAndRemove(ctx.params.id)
        ctx.body = new SuccessModel(comment)
    }

}



module.exports = new CommentCtl()